package com.test.dubbo.server;


import com.test.dubbo.DubboProtocol;
import com.test.dubbo.service.impl.UserServiceImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.springframework.stereotype.Component;


@Component
public class NettyServerHandler extends SimpleChannelInboundHandler<DubboProtocol>
{


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }



    @Override
    protected void channelRead0 (ChannelHandlerContext ctx, DubboProtocol dubboProtocol)
            throws Exception
    {
        //获取客户端发送的消息，并调用服务
        System.out.println("channelRead0");
        String s = new String (dubboProtocol.getContent (), "utf-8");
        String[] split = s.split ("#");
        System.out.println (split[0]);
        System.out.println (split[1]);
        System.out.println (split[2]);
        if("UserService".equals (split[0])){
            UserServiceImpl userService = new UserServiceImpl ();
            String name =  userService.getUserNameById (Long.parseLong (split[2]));
            DubboProtocol ack = new DubboProtocol (name);
            ctx.writeAndFlush (ack);
        }
    }
}
